import 'package:flutter/material.dart';

typedef OnBackCallback = void Function(BuildContext context);
typedef OnForwardCallback = void Function(BuildContext context);
typedef OnAddCallback = void Function(BuildContext context, bool longPress);
typedef OnRefreshCallback = void Function(BuildContext context);
typedef OnToggleFavoriteCallback = void Function(
    BuildContext context, bool currentIsFavourite);

/// 浏览器导航栏
class BrowserNavigationBar extends StatelessWidget {
  final bool isCanGoBack;
  final bool isCanForward;
  final bool isFavorite;

  final OnBackCallback? onBackCallback;
  final OnForwardCallback? onForwardCallback;
  final OnAddCallback? onAddCallback;
  final OnRefreshCallback? onRefreshCallback;
  final OnToggleFavoriteCallback? onToggleFavoriteCallback;

  const BrowserNavigationBar(
      this.isCanGoBack, this.isCanForward, this.isFavorite,
      {this.onBackCallback,
      this.onForwardCallback,
      this.onAddCallback,
      this.onRefreshCallback,
      this.onToggleFavoriteCallback,
      Key? key})
      : super(key: key);

  /// 根据是否可用，返回按钮的颜色
  Color _getIconButtonColor(bool isEnable) {
    if (isEnable) {
      return Colors.black;
    } else {
      return Colors.grey;
    }
  }

  /// 根据是否喜欢，返回按钮的图标
  IconData getFavoriteIconData(bool isFavorite) {
    if (isFavorite) {
      return Icons.favorite;
    } else {
      return Icons.favorite_border;
    }
  }

  /// 根据是否喜欢，返回按钮颜色
  Color _getFavoriteButtonColor(bool isFavorite) {
    if (isFavorite) {
      return Colors.red;
    } else {
      return Colors.black;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Divider(
          height: 1,
          indent: 0,
          color: Color(0xFFDDDDDD),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            IconButton(
                icon: Icon(
                  Icons.arrow_back_ios,
                  color: _getIconButtonColor(isCanGoBack),
                ),
                onPressed: () {
                  onBackCallback?.call(context);
                }),
            IconButton(
                icon: Icon(
                  Icons.arrow_forward_ios,
                  color: _getIconButtonColor(isCanForward),
                ),
                onPressed: () {
                  onForwardCallback?.call(context);
                }),
            GestureDetector(
              child: IconButton(
                icon: Icon(
                  Icons.add,
                  color: _getIconButtonColor(true),
                ),
                onPressed: () {
                  onAddCallback?.call(context, false);
                },
              ),
              onLongPress: () {
                onAddCallback?.call(context, true);
              },
            ),
            IconButton(
                icon: Icon(
                  Icons.refresh,
                  color: _getIconButtonColor(true),
                ),
                onPressed: () {
                  onRefreshCallback?.call(context);
                }),
            IconButton(
                icon: Icon(
                  getFavoriteIconData(isFavorite),
                  color: _getFavoriteButtonColor(isFavorite),
                ),
                onPressed: () {
                  onToggleFavoriteCallback?.call(context, isFavorite);
                }),
          ],
        )
      ],
    );
  }
}
